package com.hua.lease.web.admin.schedule;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.hua.lease.model.entity.LeaseAgreement;
import com.hua.lease.model.enums.LeaseStatus;
import com.hua.lease.web.admin.service.LeaseAgreementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class ScheduledTasks {

	@Autowired
	private LeaseAgreementService leaseAgreementService;

	@Scheduled(cron = "0 0 0 * * *")    /*秒、分、时、天、月、周*/
	public void checkLeaseStatus() {

		LambdaUpdateWrapper<LeaseAgreement> updateWrapper = new LambdaUpdateWrapper<>();
		updateWrapper.le(LeaseAgreement::getLeaseEndDate, new Date());
		updateWrapper.eq(LeaseAgreement::getStatus, LeaseStatus.EXPIRED);
		updateWrapper.in(LeaseAgreement::getStatus, LeaseStatus.SIGNED, LeaseStatus.WITHDRAWING);

		leaseAgreementService.update(updateWrapper);
	}
}
